package cn.qqtheme.framework.picker;

import android.content.Context;
import androidx.annotation.NonNull;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.efounder.mobilecomps.R;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import cn.qqtheme.framework.popup.ConfirmPopup;
import cn.qqtheme.util.ScreenUtils;

/**
 * 单项选择器
 *
 * @author 李玉江[QQ:1032694760]
 * @since 2015/9/29
 */
public class OptionListViewPicker extends WheelPicker implements ConfirmPopup.OnCheckFilterListener{
    protected List<String> options = new ArrayList<String>();
    private OnOptionPickListener onOptionPickListener;
    private int selectedOption = 0;
    private String label = "";
    MyAdapter myAdapter = new MyAdapter(context);

    /**
     * Instantiates a new Option picker.
     *
     * @param context the context
     * @param options  the options
     */
    public OptionListViewPicker(Context context, String[] options) {
        super(context);
        this.options.addAll(Arrays.asList(options));
        this.optionTemp.addAll(Arrays.asList(options));
        setOnCheckFilterListener(this);
    }

    public OptionListViewPicker(Context context, ArrayList<String> options) {
        super(context);
        setFilterVisible(true);
        this.options.addAll(options);
        this.optionTemp.addAll(options);
        setOnCheckFilterListener(this);
    }

    /**
     * 设置显示的单位，如身高为cm、体重为kg
     */
    public void setLabel(String label) {
        this.label = label;
    }

    /**
     * 设置默认选中的项的索引
     */
    public void setSelectedIndex(int index) {
        if (index >= 0 && index < options.size()) {
            selectedOption = index;
        }
    }

    /**
     * 设置默认选中的项
     */
    public void setSelectedItem(String option) {
        setSelectedIndex(options.indexOf(option));
    }

    public void setOnOptionPickListener(OnOptionPickListener listener) {
        this.onOptionPickListener = listener;
    }


    @Override
    @NonNull
    protected View makeCenterView() {
        if (options.size() == 0) {
            throw new IllegalArgumentException("please initial options at first, can't be empty");
        }
        LinearLayout layout = new LinearLayout(context);
        layout.setOrientation(LinearLayout.HORIZONTAL);
        layout.setGravity(Gravity.CENTER);
        ListView optionView = new ListView(context);
//        WheelView optionView = new WheelView(context);
//        optionView.setLayoutParams(new LinearLayout.LayoutParams(WRAP_CONTENT, WRAP_CONTENT));
//        optionView.setTextSize(textSize);
//        optionView.setTextColor(textColorNormal, textColorFocus);
//        optionView.setLineVisible(lineVisible);
//        optionView.setLineColor(lineColor);
//        optionView.setOffset(offset);
//        layout.addView(optionView);
//        TextView labelView = new TextView(context);
//        labelView.setLayoutParams(new LinearLayout.LayoutParams(WRAP_CONTENT, WRAP_CONTENT));
//        labelView.setTextColor(textColorFocus);
//        labelView.setTextSize(textSize);
//        layout.addView(labelView);
//        if (!TextUtils.isEmpty(label)) {
//            labelView.setText(label);
//        }
//        optionView.setItems(options, selectedOption);
//        optionView.setOnWheelViewListener(new WheelView.OnWheelViewListener() {
//            @Override
//            public void onSelected(boolean isUserScroll, int selectedIndex, String item) {
//                selectedOption = selectedIndex;
//            }
//        });
//        optionView.setLayoutParams(new LinearLayout.LayoutParams(MATCH_PARENT, 600));
        optionView.setLayoutParams(new LinearLayout.LayoutParams(MATCH_PARENT, ScreenUtils.heightPixels(context)/3));

        optionView.setAdapter(myAdapter);
        layout.addView(optionView);

        optionView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                selectedOption = position;
                myAdapter.notifyDataSetChanged();
            }
        });
        return layout;
    }

    @Override
    public void onSubmit() {
        if (onOptionPickListener != null&&options.size()>0) {
            String resultStr = options.get(selectedOption);
            int pos = optionTemp.indexOf(resultStr);
            onOptionPickListener.onOptionPicked(pos, resultStr);
            Log.i("option", "pos:"+pos+"onSubmit: "+options.size()+",selectedOption:"+selectedOption+",options.get(selectedOption):"+options.get(selectedOption));
        }
    }

    /**
     * Gets selected option.
     *
     * @return the selected option
     */
    public String getSelectedOption() {
        return options.get(selectedOption);
    }

    /**
     * Get the selected position.
     *
     * @return the selected position
     */
    public int getSelectedPosition() {
        return selectedOption;
    }

    /**
     * 过滤筛选后  回复原来数据帮助变量
     */
    List<String> optionTemp = new ArrayList<String>();
    @Override
    public void onSearch(String filter) {
        options.clear();
        myAdapter.notifyDataSetChanged();
        if(!"".equals(filter)){
            List<String> option = new ArrayList<String>();
            for(String str1 : optionTemp){
                if(str1.contains(filter)){
                    option.add(str1);
                }
            }
            options.addAll(option);
        }else{//还原原来数据
        options.addAll(optionTemp);
        }
        myAdapter.notifyDataSetChanged();
    }

    /**
     * The interface On option pick listener.
     */
    public interface OnOptionPickListener {

        /**
         * On option picked.
         *
         * @param position the position you selected
         * @param option   the option
         */
        void onOptionPicked(int position, String option);

    }

    public final class ViewHolder{

        public TextView content;

    }


    public class MyAdapter extends BaseAdapter {
        private LayoutInflater mInflater;


        public MyAdapter(Context context){
            this.mInflater = LayoutInflater.from(context);
        }
        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return options.size();
        }

        @Override
        public Object getItem(int arg0) {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public long getItemId(int arg0) {
            // TODO Auto-generated method stub
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            ViewHolder holder = null;
            if (convertView == null) {

                holder=new ViewHolder();

                convertView = mInflater.inflate(R.layout.optionlistviewitem, null);

                holder.content = (TextView)convertView.findViewById(R.id.txt);

                convertView.setTag(holder);

            }else {

                holder = (ViewHolder)convertView.getTag();
            }
            if (position == selectedOption) {
                convertView.setBackgroundColor(context.getResources().getColor(R.color.blue_btn_bg_pressed_color));
            }
            else {
                convertView.setBackgroundColor(context.getResources().getColor(R.color.white));
            }
            holder.content.setText(options.get(position).trim());
            return convertView;
        }

    }
}
